package com.vdong.shardingorder.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vdong.shardingorder.entity.VdGoods;
import com.vdong.shardingorder.entity.VdOrder;
import com.vdong.shardingorder.entity.VdOrderDetails;
import com.vdong.shardingorder.mapper.VdOrderMapper;
import com.vdong.shardingorder.service.IVdOrderDetailsService;
import com.vdong.shardingorder.service.IVdOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vdong.shardingorder.utils.SnowflakeIdWorkerUtils;
import com.vdong.shardingorder.vo.order.req.ListOrderReqVO;
import com.vdong.shardingorder.vo.order.req.SaveOrderReqVO;
import com.vdong.shardingorder.vo.order.resp.OrderDetailRespVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 宋发准
 * @since 2021-08-31
 */
@Service
@DS("order_db")
public class VdOrderServiceImpl extends ServiceImpl<VdOrderMapper, VdOrder> implements IVdOrderService {


    @Autowired
    private IVdOrderDetailsService orderDetailsService;

    @Override
    @Transactional
    public Boolean saveOrder(SaveOrderReqVO req) {
        long id = SnowflakeIdWorkerUtils.genId();
        VdOrder order = new VdOrder();
        BeanUtil.copyProperties(req, order);
        order.setId(id);
        save(order);
        List<SaveOrderReqVO.Detail> list = req.getDetails();
        for (SaveOrderReqVO.Detail detail : list) {
            VdOrderDetails details = new VdOrderDetails();
            BeanUtil.copyProperties(detail, details);
            details.setId(SnowflakeIdWorkerUtils.genId());
            details.setUserId(req.getUserId());
            details.setOrderId(id);
            orderDetailsService.save(details);
        }

        return true;
    }

    @Override
    public OrderDetailRespVO orderDetail(Long userId, Long id) {
        LambdaQueryWrapper<VdOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VdOrder::getUserId, userId);
        wrapper.eq(VdOrder::getId, id);
        VdOrder one = getOne(wrapper);

        LambdaQueryWrapper<VdOrderDetails> wrapper2 = new LambdaQueryWrapper<VdOrderDetails>();
        wrapper2.eq(VdOrderDetails::getUserId, userId);
        wrapper2.eq(VdOrderDetails::getOrderId, id);
        List<VdOrderDetails> list = orderDetailsService.list(wrapper2);

        OrderDetailRespVO vo = new OrderDetailRespVO();
        BeanUtil.copyProperties(one, vo);
        vo.setOrderId(one.getId());

        ArrayList<OrderDetailRespVO.Detail> details = new ArrayList<>();
        for (VdOrderDetails orderDetails : list) {
            OrderDetailRespVO.Detail detail = new OrderDetailRespVO.Detail();
            BeanUtil.copyProperties(orderDetails, detail);
            detail.setDetailId(orderDetails.getId());
            details.add(detail);

        }
        vo.setDetails(details);
        return vo;
    }

    @Override
    public IPage<VdOrder> listOrder(ListOrderReqVO req) {
        LambdaQueryWrapper<VdOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VdOrder::getUserId, req.getUserId());
        IPage<VdOrder> page = new Page<>(req.getPage(), req.getSize());
        IPage<VdOrder> p = page(page, wrapper);
        return p;
    }
}
